//
//  KZEventsViewController.m
//  xiaorizi
//
//  Created by kun on 15/10/24.
//  Copyright © 2015年 origin. All rights reserved.
//

#import "KZEventsViewController.h"

//用于头部下拉放大
#import "UINavigationBar+Status.h"
#import "UIImageView+WebCache.h"
#import "UIViewAdditions.h"
#import "KZTableHeaderVIew.h"
#import "KZEventCell.h"
#import "KZContentFile.h"
#import "KZMapViewController.h"

@interface KZEventsViewController ()

@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic)  UIImageView *imageView;

@property (weak, nonatomic) IBOutlet UIButton *collectButton;


@end

@implementation KZEventsViewController

- (IBAction)leftItemBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}



//实现分享


//实现收藏

- (IBAction)dealCollectClicked:(id)sender {

    UIButton *btn = sender;
    btn.selected = !btn.selected;
    KZContentFile *manger = [KZContentFile shareInstance];
   // KZContentData *dataModel = [[KZContentData alloc]initWithModel:_model];
    //实现model的归档
    if(btn.selected)
    {
        //初始化数据管理对象
        _model.isCollect = YES;
        [manger addContentData:_model];
        //这里应该实现一个通知 通知login重新加载我的数据
        
    }
    else
    {
         _model.isCollect = NO;
        [manger removeContentData:_model];
        //这里实现一个通知用来通知实现数据的更新
    }
    
}


//添加tabbar隐藏


//-(void)viewWillDisappear:(BOOL)animated
//{
////    [super viewWillDisappear:animated];
////    self.tabBarController.tabBar.hidden = NO;
//    
//    
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController.navigationBar nav_setBackgroundColor:[UIColor clearColor]];
    self.tableView.delegate = self;
    
    //button按钮显示
    _collectButton.selected = _model.isCollect;
    
}



//控制头视图的出现消失
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    self.tabBarController.tabBar.hidden = NO;
    
    [self.navigationController.navigationBar nav_clear];
    self.tableView.delegate = nil;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    //加载头部视图
    [self createHeaderView];
    
    CGFloat H = self.view.height;
    self.tableView.rowHeight =  H - 64 - 50;
}

-(void)createHeaderView
{

    UIImageView * imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(0,-64,self.view.width, 200);
    [imageView sd_setImageWithURL:[NSURL URLWithString:_model.imgs]];
    _imageView = imageView;
    _imageView.contentMode = UIViewContentModeScaleAspectFill;
    [_contentView addSubview:imageView];
    _contentView.backgroundColor = [UIColor whiteColor];
    _contentView.frame = CGRectMake(0, 0, self.view.width, 134);
    
    
    self.tableView.tableHeaderView = _contentView;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete implementation, return the number of rows
    return 1;
}

-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    //创建一个uiview用于加载组头视图
    KZTableHeaderVIew *view = [KZTableHeaderVIew tableHeaderView];
    return view;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KZEventCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eventscell" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.model = self.model;
    
    [cell setChangeCellHeightBlock:^(KZEventCell *cell ) {
        self.tableView.rowHeight = 300;
        [self.tableView reloadData];
    }];
    
    [cell setRestoreCellHeightBlock:^(KZEventCell *cell) {
        self.tableView.rowHeight = self.view.height - 64 - 50;
        [self.tableView reloadData];
    }];
    
    return cell;
}


//添加 地址导航
- (IBAction)addressClicked:(id)sender {
    
    
    
}
//
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    KZEventCell *cell
//}



-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @"header";
}


#pragma 用于下拉图片放大

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    self.navigationController.navigationBar.alpha = 0;
    
   // NSLog(@"1----->%@",NSStringFromCGPoint(scrollView.contentOffset));
    UIColor * color = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:249/255.0 alpha:1];
    
    
    CGFloat offSetY = scrollView.contentOffset.y;
    
    if (offSetY > -64)
    {
        CGFloat alpha =   (64+offSetY)/136;

        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
     
        if(offSetY > 72)
        {
            //这一句放入口会造成异常错误
            KZEventCell *cell = [[self.tableView visibleCells]lastObject];

            cell.webView.userInteractionEnabled = YES;
        }
        
    } else if(offSetY < -64)
    {
        CGFloat H = -64 -offSetY;
        _imageView.frame = CGRectMake(0, -64- H, self.view.width, 200 +H);
        
    }
    else
    {
        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}


//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    //    self.navigationController.navigationBar.alpha = 0;
//    
//    NSLog(@"----->%@",NSStringFromCGPoint(scrollView.contentOffset));
//    UIColor * color = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:249/255.0 alpha:1];
//    
//    KZEventCell *cell = [[self.tableView visibleCells]lastObject];
//    //NSLog(@"--->%lf",cell.webView.scrollView.contentOffset.y);
//    if(cell.webView.scrollView.contentOffset.y == 0)
//    {
//        cell.webView.userInteractionEnabled = NO;
//    }
//    
//    CGFloat offSetY = scrollView.contentOffset.y;
//    
//    if (offSetY > -64)
//    {
////      //  CGFloat offSetH = offSetY < 0 ? 64 + offSetY : ;
////        CGFloat alpha =   (64+offSetY)/136;
////        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
////        if(offSetY > 72)
////        {
////            KZEventCell *cell = [[self.tableView visibleCells]lastObject];
////            cell.webView.userInteractionEnabled = YES;
////
////            
////        }
//    } else if(offSetY < -64)
//    {
////       // _imageView.contentMode = UIViewContentModeScaleAspectFill;
////        CGFloat H =  - 64 -offSetY;
////        _imageView.frame = CGRectMake(0,-64 -H, self.view.width, 200 + H);
//    }
//    else
//    {
//        [self.navigationController.navigationBar nav_setBackgroundColor:[color colorWithAlphaComponent:0]];
//    }
//    
//}
//






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    KZMapViewController *mapView = segue.destinationViewController;
    mapView.model = self.model;
}


@end
